﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Parts_RegisterProvider : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["RandomKey"] == null)
        {
            Session["RandomKey"] = Path.GetRandomFileName().Replace(".", "").ToUpper();
        }

        if (Request.Form["txtRegisterProviderUsername"] != null 
            && Request.Form["txtRegisterProviderPassword"] != null
            && Request.Form["txtRegisterProviderName"] != null
            && Request.Form["txtRegisterPartnerUsername"] != null
            && Request.Form["txtRegisterPartnerPassword"] != null
            && Request.Form["txtRegisterCreditCardNumber"] != null
            && Request.Form["txtRegisterLinkWS"] != null           
            && Request.Form["act"] != null)
        {
            NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();
            var query = from n in db.NhaCungCaps
                        where n.ProviderID.Trim().ToLower().Equals(Request.Form["txtRegisterProviderUsername"].Trim().ToLower())
                        select n;

            if (query.Count() > 0)
            {
                NhaCungCap nccTest = query.Single<NhaCungCap>();
                if (nccTest != null)
                {
                    lblRegister.Visible = true;
                    lblRegister.Text = "Tên đăng nhập đã tồn tại!<br/><br/>";

                    return;
                }
            }
            NhaCungCap ncc = new NhaCungCap();
            ncc.ProviderID = Request.Form["txtRegisterProviderUsername"];            
            //ncc.ProviderPwd =MD5Hash.GetMd5Hash(Request.Form["txtRegisterProviderPassword"]);
            ncc.ProviderPwd = Request.Form["txtRegisterProviderPassword"];
            ncc.ProviderName = Request.Form["txtRegisterProviderName"];
            ncc.PartnerID = Request.Form["txtRegisterPartnerUsername"];            
            //ncc.PartnerPwd = MD5Hash.GetMd5Hash(Request.Form["txtRegisterPartnerPassword"]);
            ncc.PartnerPwd = Request.Form["txtRegisterPartnerPassword"];
            ncc.CreditCardNumber = Request.Form["txtRegisterCreditCardNumber"];
            ncc.LinkWS = Request.Form["txtRegisterLinkWS"];
            db.NhaCungCaps.InsertOnSubmit(ncc);
            db.SubmitChanges();
            lblRegister.Visible = true;
            lblRegister.Text = string.Format("Chúc mừng <strong>{0}</strong>.<br/>Bạn đã đăng ký thành công!<br/><br/>", ncc.ProviderID);
        }
        else if (Request.Form["txtLoginUsername"] != null && Request.Form["txtLoginPassword"] != null)
        {
            NhaKinhDoanhDataContext db = new NhaKinhDoanhDataContext();
            //string pass = MD5Hash.GetMd5Hash(Request.Form["txtLoginPassword"].Trim());
            string pass = Request.Form["txtLoginPassword"].Trim();
            var query = from n in db.NhaCungCaps
                        where (n.ProviderID.Trim().ToLower().Equals(Request.Form["txtLoginUsername"].Trim().ToLower())
                        && n.ProviderPwd.Trim().Equals(pass))
                        select n;
            if(query.Count()>0)
            {
                NhaCungCap ncc = query.Single<NhaCungCap>();
                if (ncc == null)
                {
                    lblLoginMsg.Visible = true;
                    lblLoginMsg.Text = "Tên đăng nhập hoặc mật khẩu không đúng!<br/><br/>";
                }
                else
                {
                    Session["TaiKhoan"] = ncc;

                    if (Session["Redirect"] != null)
                    {
                        Response.Redirect(Session["Redirect"].ToString());
                    }
                    else
                    {
                        Response.Redirect("~/ProviderAccount.aspx");
                    }
                }

            }
            else
            {
                lblLoginMsg.Visible = true;
                lblLoginMsg.Text = "Tên đăng nhập hoặc mật khẩu không đúng!<br/><br/>";
            }
               
          
           
        }
    }
}